#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <istream>
#include <map>
#include <string>
#include <unordered_set>
#include <vector>
using ll = int64_t;

const ll maxn = 5e4+5;
ll n,l,allweight;
struct P{
    ll w,x,d;
    inline bool operator<(const P&other)const{
        return x<other.x;
    }
}c[maxn];
struct D{
    ll d,idx;
    inline bool operator<(const D&other)const{
        return d<other.d;
    }
}dis[maxn];
std::vector<ll> after,rafter,tmp;
std::unordered_set<ll> vis;

static inline ll merge(std::vector<ll>&num,std::vector<ll>&tmp,ll l,ll mid,ll r){
    ll cnt=0;
    ll i=l,j=mid+1,k=l;
    while(i<=mid&&j<=r){
        if(num[i]<num[j]){
            tmp[k++]=num[i++];
        }else{
            cnt+= mid-i+1;
            tmp[k++]=num[j++];
        }
    }
    while(i<=mid)tmp[k++]=num[i++];
    while(j<=r)tmp[k++]=num[j++];
    for(ll i=l;i<=r;i++){
        num[i]=tmp[i];
    }
    return cnt;
}

static inline ll mergesort(std::vector<ll>&num,std::vector<ll>&tmp,ll l,ll r){
    ll cnt=0;
    if(l<r){
        ll mid=(l+r)/2;
        cnt+=mergesort(num, tmp, l, mid);
        cnt+=mergesort(num, tmp, mid+1, r);
        cnt+=merge(num,tmp,l,mid,r);
    }
    return cnt;
}

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>n>>l;
    after.reserve(n+1);
    tmp.resize(n+1);
    for(ll i=1;i<=n;i++){
        std::cin>>c[i].w>>c[i].x>>c[i].d;
        allweight+=c[i].w;
    }
    std::sort(c+1,c+1+n);
    for(ll i=1;i<=n;i++){
        dis[i].idx=i;
        if(c[i].d==1){
            dis[i].d=l-c[i].x;
        }else{
            dis[i].d=c[i].x;
        }
    }
    std::sort(dis+1,dis+1+n);
    ll outweight=0;
    ll pout=0,lout=1,rout=n;
    for(ll i=1;i<=n;i++){
        if(c[dis[i].idx].d==1){
            outweight+=c[rout--].w;
        }else{
            outweight+=c[lout++].w;
        }
        // outweight+=c[dis[i].idx].w;
        // printf("%lld is out\n",c[dis[i].idx].x);
        pout=dis[i].d;
        vis.emplace(dis[i].idx);
        if(outweight*2>=allweight){
            break;
        }
    }
    // printf("lnum=%lld, rnum=%lld\n",lnum,rnum);
    // std::cout<<"after:\n";
    // for(ll i:after){
    //     std::cout<<i<<" ,";
    // }
    // std::cout<<"\n";
    after.push_back(0);
    for(ll i=1;i<=n;i++){
        ll na=c[i].x+c[i].d*pout;
        after.push_back(na);
    }
    const ll ans = mergesort(after, tmp, 1, n);
    std::cout<<ans<<"\n";
}